<?php
class tx_branchinfo extends mod_user_task {
	var $charWidth=60;
	var $curBranch;
	
	function modMenu()	{
		global $LANG;
		return Array();
	}
	function overview_main()	{
		$icon = '<img src="'.$this->backPath.t3lib_extMgm::extRelPath("user_farm_branches").'icon_user_farmbranches_branches.gif" width="18" height="16" />';
		return $this->mkMenuConfig($icon.$this->headLink("tx_branchinfo"));

	}
	function main() {
		global $SOBE,$BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

		$LANG->includeLLFile(t3lib_div::getFileAbsFileName('EXT:user_farm_branches/mod_branch_info/locallang.xml'));
		$this->pObj->doc->styleSheetFile2 = t3lib_extMgm::extRelPath('user_farm_branches').'mod_branch_info/resources/styles.css';
		
		$additionalWhere = '';
		if (!$BE_USER->isAdmin())
		{
			$additionalWhere .= '(user_farm_branches.leader_id = '.$BE_USER->user["uid"];
			if ($BE_USER->groupList)
			{
				$additionalWhere .= " OR user_farm_branches.group_id IN (".$BE_USER->groupList."))";
			}
			else
			{
				$additionalWhere .= ')'; 
			}
			$additionalWhere .= t3lib_BEfunc::deleteClause('user_farm_branches');
		}

		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'user_farm_branches', $additionalWhere, '', '', '1');
		
		$output = '';
		if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0)
		{
			$this->curBranch = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
			$output = $this->getBranchInfo();
		}
		else
		{
			$output = $this->displayError();
		}
		return $output;
		
	}
	
	function getBranchInfo()
	{
		global $LANG;
	
		$output = '';
		$output .= $this->pObj->doc->header('Информация о филиале');
		
		$output .= $this->getDescriptionGroup($LANG->getLL('branch_info.commonDescription'), 'name,region_id,description');
		$output .= $this->getDescriptionGroup($LANG->getLL('branch_info.contactInfo'), 'zip,city,address,phone,email');
		$output .= $this->getDescriptionGroup($LANG->getLL('branch_info.registrationInfo'), 
			'invoice_company,invoice_inn,invoice_address,invoice_phones,invoice_director,invoice_chief_accountant');
		$output .= $this->getDescriptionGroup($LANG->getLL('branch_info.bankInfo'), 
			'invoice_bank_name,invoice_bik,invoice_kpp,invoice_settlement_account,invoice_cor_account');
		$output .= $this->getDescriptionGroup($LANG->getLL('branch_info.deliveryInfo'), 'delivery');
		
		return $output;
	
	}
	
	function getDescriptionGroup($title, $fieldList)
	{
		global $LANG;
		
		$fields = t3lib_div::trimExplode(',', $fieldList);
		
		$output = '
			<table class="branch-info-block">
				<caption>'.$title.'<a href="#" onclick="'.t3lib_BEfunc::editOnClick('&edit[user_farm_branches]['.$this->curBranch['uid'].']=edit&columnsOnly='.$fieldList,
					$this->pObj->backPath).'"><img'.t3lib_iconWorks::skinImg(
						$this->pObj->backPath, 'gfx/edit2.gif', ' width="16" height="16"').
					'title="Редактировать"></a></caption>';
		foreach($fields as $field)
		{
			$output .= '
				<tr>
					<td class="branch-info-label-column">'.$LANG->getLL('branch_info.'.$field).'</td>
					<td class="branch-info-value-column">'.$this->getSingleFieldValue($field).'</td>
				</tr>';
		}
		$output .= '</table>';
		
		return $output;
	}
	
	function getSingleFieldValue($field)
	{
		switch($field)
		{
			case 'region_id':
				$region = t3lib_BEfunc::getRecord('user_farm_branches_regions', $this->curBranch['region_id']);	
				return $region['name'];
				break;
			default:
				return $this->curBranch[$field];
		}
	}
	
	function displayError()
	{
		global $LANG;
		
		$output = '';
		$output .= $this->pObj->doc->header('Информация о филиале');
		$output .= '<div class="branchinfo-error">'.$LANG->getLL('branch_info.error_info').'</div>';
		return $output;
	}
}

if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/user_farm_branches/mod_branch_info/class.tx_branchinfo.php"])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/user_farm_branches/mod_branch_info/class.tx_branchinfo.php"]);
}
?>